<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<jsp:include page="/share/top.jsp"/>
<%
    String path = request.getContextPath();
%>

<div class="layui-inline">
    分类名称
    <div class="layui-input-inline">
        <input name="type_name" id="type_name" lay-verify="required" autocomplete="off" class="layui-input" type="text">
    </div>
</div>
<div class="layui-inline" style="margin-left:50px">
    <div class="layui-input-inline">
        <button class="layui-btn layui-btn-normal layui-btn-radius" onclick="queryResourceList();">查询</button>
    </div>
    <div class="layui-input-inline" style="margin-left:20px">
        <button class="layui-btn" onclick="addResource(-1)">
            <i class="layui-icon">&#xe608;</i>添加内容
        </button>
    </div>
</div>
<div class="table-responsive">
    <table class="table layui-table" style="table-layout:fixed;word-wrap:break-word;" cellpadding="0" cellspacing="0">
        <col width="150px"/>
        <col width="150px"/>
        <col/>
        <col/>
        <col width="150px"/>
        <col width="150px"/>
        <col width="150px"/>
        <thead>
        <tr>
            <th>分类</th>
            <th>标题</th>
            <th>图片</th>
            <th>内容地址</th>
            <th>创建人</th>
            <th>创建时间</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="tbody">

        </tbody>
    </table>
</div>
<div id="page"></div>


<jsp:include page="/share/foot.jsp"/>
<script type="text/javascript" src="<%=path%>/js/core/helper.js"></script>
<SCRIPT language="javascript">
    $(document).ready(function () {
    });

    layui.use(['layer', 'form', 'upload', 'laypage'], function () {
        queryResourceList();
    });

    var modelParam = {};
    modelParam.pageSize = 10;

    function queryResourceList(page) {
        var param = {};
        param.type_name = $('#type_name').val();
        param.pageSize = modelParam.pageSize;
        param.page = 1;

        postJson("<%=path%>/cms/resource/queryResourceList.x", $.param(param), function (ret) {
            var data = ret.data;
            $('#tbody').empty();
            render(data.list);
            layui.laypage({
                cont: 'page'
                , pages: Math.ceil(data.size / modelParam.pageSize)
                , jump: function (row, first) {
                    if (!first) {
                        param.page = row.curr;
                        queryPage(param);
                    }
                }
            });
        });
    }

    function queryPage(param) {
        postJson("<%=path%>/cms/resource/queryResourceList.x", $.param(param), function (ret) {
            $('#tbody').empty();
            render(ret.data.list);
        });
    }

    function render(list) {
        modelParam.list = list;
        for (var i in list) {
            var row = list[i];
            var tr = $("<tr></tr>");
            tr.appendTo($('#tbody'));
            $("<td>" + row.type_name + "</td>").appendTo(tr);
            $("<td>" + row.title + "</td>").appendTo(tr);
            $("<td style='word-wrap:break-word;'>" + row.icon_url + "</td>").appendTo(tr);
            $("<td style='word-wrap:break-word;'>" + row.content_url + "</td>").appendTo(tr);
            $("<td>" + row.creator + "</td>").appendTo(tr);
            $("<td>" + row.create_time + "</td>").appendTo(tr);

            var last = $("<td class='t7'></td>");
            $("<button class='layui-btn layui-btn-mini layui-btn-normal' onclick='addResource(" + i + ")'>修改</button>").appendTo(last);
            $("<button class='layui-btn layui-btn-mini layui-btn-danger' onclick='deleteResource(" + i + ")'>删除</button>").appendTo(last);
// 		$("<button class='layui-btn layui-btn-mini layui-btn-normal' onclick='queryResource("+i+")'>查看</button>").appendTo(last);
            last.appendTo(tr);
        }
    }

    function addResource(row) {
        var param = "";
        if (row != -1) {
            param = $.param(modelParam.list[row]);
        }

        layui.layer.open({
            title: '资源内容添加',
            type: 2,
            area: ['700px', '450px'],
            fixed: false, //不固定
            maxmin: true,
            content: '<%=path%>/cms/resource/goResourceAddPage.x?' + param
        });
    }

    function deleteResource(row) {
        var id = modelParam.list[row].resource_id;
        layer.confirm('确定要删除？', {
            btn: ['确定', '取消']
        }, function () {
            postJson("<%=path%>/cms/resource/deleteResource.x", $.param({'resource_id': id}));
            queryResourceList();
        });
    }

</SCRIPT>